package com.rzjm.exercise.module.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.rzjm.exercise.common.base.controller.BaseController;
import com.rzjm.exercise.common.base.data.DataTableLayUI;
import com.rzjm.exercise.common.config.ProjectUrlConfig;
import com.rzjm.exercise.common.log.annotation.Log;
import com.rzjm.exercise.common.utils.R;
import com.rzjm.exercise.module.domain.EventsType;
import com.rzjm.exercise.module.service.EventsTypeService;
import com.rzjm.exercise.module.service.ShopService;
import com.rzjm.exercise.module.service.WxUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;


@Controller
@RequestMapping(value = "/module/eventsType",produces ="application/json;charset=UTF-8")
public class EventsTypeController extends BaseController {
    @Resource
    private EventsTypeService eventsTypeService;


    @Log("查询排查项分类页面")
    @GetMapping("")
    @RequiresPermissions("module:eventsType:eventsType")
    String eventsType(Model model) {
        return "module/eventsType/list";
    }


    @Log("查询排查项分类页面")
    @ResponseBody
    @GetMapping("/list")
    public DataTableLayUI<EventsType> list(DataTableLayUI<EventsType> condition) {
        return eventsTypeService.getListPage(condition);
    }


    @Log("新增排查项分类页面")
    @GetMapping("/add")
    @RequiresPermissions("module:eventsType:add")
    String add(Model model) {
        QueryWrapper<EventsType> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id",0);
        List<EventsType> typeList = eventsTypeService.list(wrapper);
        model.addAttribute("typeList", typeList);
        EventsType eventsType = new EventsType();
        model.addAttribute("eventsType", eventsType);
        return "module/eventsType/detail";
    }

    @Log("编辑排查项分类页面")
    @GetMapping("/edit/{id}")
    @RequiresPermissions("module:eventsType:edit")
    String edit(@PathVariable("id") Integer id, Model model) {
        EventsType eventsType = eventsTypeService.findById(id);
        model.addAttribute("eventsType", eventsType);
        return "module/shop/detail";
    }




    /**
     * 修改
     */
    @Log("更新排查项分类")
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("module:eventsType:edit")
    public R update(@Valid EventsType eventsType) {
        eventsTypeService.updateById(eventsType);
        return R.ok();
    }


    /**
     * 删除
     */
    @Log("删除排查项分类")
    @PostMapping("/remove")
    @ResponseBody
    @RequiresPermissions("module:eventsType:remove")
    public R remove(Integer id) {
        if (eventsTypeService.removeById(id)) {
            return R.ok();
        }
        return R.error();
    }

    @ResponseBody
    @PostMapping(value = "/parentIdChange")
    R parentIdChange(@RequestParam("parentId") Integer parentId) {
        QueryWrapper<EventsType> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id",parentId);
        wrapper.eq("state",1);
        List<EventsType> list = eventsTypeService.list(wrapper);
        return R.ok(0,list);
    }
}
